﻿@using IOA.MES.DataAccess.Enum
@{
    ViewBag.Title = "编辑工单BOM";
    Layout = "~/Views/Shared/_LayoutPage.cshtml";
    var siteType = (SiteType)ViewBag.SiteType;
    var workOrderNo = (string)ViewBag.WorkOrderNo;
}

@section Contents{
    <style>
        html {
            background-color: #FFF;
        }

        .layui-form-item .layui-input-inline {
            width: 300px;
        }

        .red {
            color: red;
        }
    </style>
}

<div id="page"></div>

<script type="text/template" id="tplPage">
    <div class="layui-card">
        <div class="layui-card-body">
            <table id="table" lay-filter="table"></table>
        </div>

        <div class="layui-form-item" style="text-align:center;padding-top: 20px;">
            <input type="hidden" name="SiteType" value="@(siteType)" />
            <input type="hidden" name="WorkOrderNo" value="@workOrderNo" />
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="submit"><i class="layui-icon">&#xe605;</i>{{lang.Field.Submit}}</button>
            <button class="layui-btn layui-btn-primary" onclick="parent.layer.close(parent.layer.getFrameIndex(window.name));"><i class="layui-icon">&#xe616;</i>{{lang.Field.Cancel}}</button>
        </div>
    </div>
</script>

@section Scripts{
    <script>
        var vm = new Vue({
            el: "#page",
            template: "#tplPage",
            data: {
                lang: JSON.parse(lang).package
            }
        });
        setTimeout(function () {
            initLayui();
        }, 50);
        function initLayui() {
            layui.use(['form', 'layer', 'index', 'laydate', 'table'], function () {
                var layer = layui.layer,
                    form = layui.form,
                    table = layui.table;

                var options = {
                    elem: '#table'
                    , data: []
                    , limit: 0
                    , cols: [[
                        { type: 'checkbox' }
                        , { field: 'BomProductCode', title: '料号' }
                        , { field: 'BomProductName', title: '名称' }
                        , { field: 'BomCount', title: '扣料数量' }
                    ]]
                };

                var data = new Array();
                queryWorkOrderBom();
                function queryWorkOrderBom() {
                    var loadId = layer.load();
                    $.ajax({
                        url: "/WorkOrder/QueryWorkOrderBoms",
                        data: {
                            workOrderNo: "@workOrderNo",
                            siteType: parseInt("@((int)siteType)")
                        },
                        complete: function () {
                            layer.close(loadId);
                        },
                        success: function (res) {
                            if (res.ErrCode != 200) {
                                layer.msg(res.ErrMsg, { icon: 5 });
                                return;
                            }
                            data = res.Data;
                            for (var i = 0; i < data.length; i++) {
                                data[i].LAY_CHECKED = data[i].Selected;
                            }
                            options.data = data;
                            options.limit = data.length;
                            table.render(options);
                        },
                        error: function (res) {
                            layer.msg(res.ErrMsg, { icon: 5 });
                        }
                    });
                }

                form.on('submit(submit)', function (data) {
                    var selectedData = table.checkStatus('table').data;
                    var productCodes = new Array();
                    for (var i = 0; i < selectedData.length; i++) {
                        productCodes.push(selectedData[i].BomProductCode);
                    }
                    submit(productCodes);
                    return false;
                });
            });

            function submit(productCodes) {
                var loadId = layer.load();
                $.ajax({
                    async: false,
                    url: "/ParaSetting/EditProductSiteBom",
                    data: {
                        workOrderNo: "@workOrderNo",
                        siteType: parseInt("@((int)siteType)"),
                        productCodes: productCodes
                    },
                    complete: function () {
                        layer.close(loadId);
                    },
                    success: function (res) {
                        if (res.ErrCode != 200) {
                            layer.msg(res.ErrMsg, { icon: 5 });
                            return;
                        }
                        layer.msg("保存成功！", { icon: 6, time: 1500 }, function () {
                            parent.layer.close(parent.layer.getFrameIndex(window.name));
                        });
                    },
                    error: function (res) {
                        layer.msg(res.ErrMsg, { icon: 5 });
                    }
                });
            }
        }
    </script>
}
